<?php
/**
 * Created by PhpStorm.
 * User: huxue
 * Date: 2018/5/28
 * Time: 11:03
 */
use vod\Request\V20170321\CreateUploadVideoRequest;
use vod\Request\V20170321\RefreshUploadVideoRequest;
use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssException;

include_once dirname(dirname(dirname(dirname(dirname((__FILE__))))))."/".'api/video/aliyun-php-sdk-core/Profile/DefaultProfile.php';
define('HOST_ADMIN_PAGESIZE','15');

class PublicClass extends M_Controller
{
    private $accessKeyId = 'LTAIEvlPaoHFZQNK';                    // 您的AccessKeyId

    private $accessKeySecret = 'Us5ShPEqXzVPCibT4qZvvYsQZAE7AD';            // 您的AccessKeySecret



    public function index(){

        $this->db->where("client","PC端");
        $this->db->where("is_del",1);
        $this->db->where("is_ground",1);
        $this->template->assign(array(
            "banners"=>$this->db->where("part","公开课")->order_by('sort asc')->get("banner_manage")->result_array()
        ));
        $total = 0 ;
        $get = $this->input->get('keywords');
        list($data, $total, $param)	= $this->_limit_page($get); // 数据库中分页查询
        $id = (int)$param['id'];
        $this->template->assign(array(
            'classes'=>$data,
            'total' => $total,
            'param' => $param,
            'stages'=>array("一年级","二年级","三年级","四年级","五年级","六年级","初一","初二","初三","高一","高二","高三"),
            'subjects'=>array("语文","数学","英语","物理","化学","生物","政治","历史","地理"),
            "course_type"=>array("1V4","1V1","免费试听","公开课","专题课"),
            'auser' => $this->db->where('id', $id)->get('public_class')->row_array(),
            'pages'	=> $this->get_gwpagination(dr_user_url('publicClass/index'), $total)
        ));

        $this->template->display("../gw/public_class.html");
    }


    private function  _limit_page($get=null){
        $page = max((int)$this->input->get_post('page'),1);
        $_total = (int)$this->input->get_post('total');
        if ($get){
            if(!$_total){
                $_total=0;
                $arr = $this->db->select("id")->like("keywords", $get, 'both')->where(array("is_ground"=>1,"is_del"=>1))->get("public_class")->result_array();

                if (count($arr) > 0){
                    foreach ($arr as $k=>$v){
                        $varr = $this->db->where("class_id",$v['id'])->get("class_video")->row_array();
                        if (!empty($varr)){
                            $_total += 1;
                        }
                    }
                }
            }
        }else{
            if(!$_total){
                $_total=0;
                $arr = $this->db->select("id")->where(array("is_ground"=>1,"is_del"=>1))->get("public_class")->result_array();
                if (count($arr) > 0){
                    foreach ($arr as $k=>$v){
                        $varr = $this->db->where("class_id",$v['id'])->get("class_video")->row_array();
                        if (!empty($varr)){
                            $_total += 1;
                        }
                    }
                }
            }
        }

        $select = $this->db;
        $_param = $this->_where($select,$get);
        $result = $this->db
            ->order_by('market_sort desc')
            ->limit(HOST_ADMIN_PAGESIZE, HOST_ADMIN_PAGESIZE * ($page - 1))
            ->get('public_class')
            ->result_array();
        $list = array();
        if(count($result)>0){
            $varr = array();
            //获取视频信息
            $access_key_id = "LTAIEvlPaoHFZQNK";
            $access_key_secret = "Us5ShPEqXzVPCibT4qZvvYsQZAE7AD";
            $regionId = 'cn-shanghai';
            $profile = DefaultProfile::getProfile($regionId, $access_key_id, $access_key_secret);
            $client = new DefaultAcsClient($profile);
            foreach ($result as $course){
                $varr = $this->db->where("class_id",$course['id'])->get("class_video")->row_array();
                if(!empty($varr)){
                    $vobj = get_play_auth($client, $varr['videoid']);
                    $varr['playauth'] = $vobj->PlayAuth;
                    $course['videos'] = $varr;
                    $list[] = $course;
                }
            }
        }
//        return array($result,$_total,$_param,$no_ground['count'],$ground['count']);
//        print_r($result);
//        exit;

        return array($list,$_total,$_param);
    }
    private function _where(&$select,$get=null) {

        $uid = (int)$this->input->get('id');
        $uid = $uid ? $uid : $this->uid;
        $search = $this->input->get('search');
        $grade = $this->input->get_post("grade");
        $subject = $this->input->get_post("subject");
        $course_type = $this->input->get_post("course_type");

        $cache_file = md5($this->duri->uri(1).$uid.$this->uid.SITE_ID.$this->input->ip_address().$this->input->user_agent()); // 缓存文件名称

        // 存在POST提交时，重新生成缓存文件
        if (IS_POST) {
            $data = $this->input->post('data');
            $this->cache->file->save($cache_file, $data, 3600);
            $search = 1;
            unset($_GET['page']);
        }
        $select->where_in('course_type', array("录播课"));
        $select->where('is_ground',1);
        $select->where("is_del",1);
        if(isset($ground)){
            $select->where(array('is_ground'=>$ground,"is_del"=>1));
        }
        if(!empty($grade)){
            $select->where('grade', $grade);
        }
        if(!empty($subject)){
            $select->where('subject', $subject);
        }
        if(!empty($course_type)){
            $select->where('course_type', $course_type);
        }
        if(!empty($get)){
            $select->like("keywords", $get, 'both');
        }

        // 存在search参数时，读取缓存文件
        if ($search == 1) {
            $data = $this->cache->file->get($cache_file);
            if (isset($data['start']) && $data['start'] && $data['start'] != $data['end']) {
                $data['end'] = $data['end'] ? $data['end'] : SYS_TIME;
                $select->where('logintime BETWEEN '.$data['start'].' AND '. $data['end']);
            }
        }
//        $data['id'] = $uid;
        $data['grade'] = $grade;
        $data['subject'] = $subject;
        $data['course_type'] = $course_type;
        return $data;
    }


}